package com.shujia.scala

import java.sql.{Connection, DriverManager, PreparedStatement, ResultSet}

object Demo10JDBC {
  def main(args: Array[String]): Unit = {
    //1、加载驱动
    Class.forName("com.mysql.jdbc.Driver")
    //2、创建链接
    val con: Connection = DriverManager.getConnection("jdbc:mysql://master:3306/student?useSSL=false", "root", "123456")
    //3、编写sql查询数据
    val stat: PreparedStatement = con.prepareStatement("select id,name,age,gender,clazz from student")
    //4、指向查询
    val resultSet: ResultSet = stat.executeQuery()
    //5、解析数据
    while (resultSet.next()) {
      val id: Int = resultSet.getInt("id")
      val name: String = resultSet.getString("name")
      val age: Int = resultSet.getInt("age")
      val gender: String = resultSet.getString("gender")
      val clazz: String = resultSet.getString("clazz")

      //打印结果
      println(s"$id\t$name\t$age\t$gender\t$clazz")
    }
    //关闭链接
    stat.close()
    con.close()
  }

}
